package com.muyu.user.controller;

import java.util.List;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.annotation.RequiresPermissions;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.muyu.user.domain.SysUserTag;
import com.muyu.user.domain.req.SysUserTagQueryReq;
import com.muyu.user.domain.model.SysUserTagQueryModel;
import com.muyu.user.domain.model.SysUserTagPageQueryModel;
import com.muyu.user.domain.resp.SysUserTagPageResp;
import com.muyu.user.domain.req.SysUserTagSaveReq;
import com.muyu.user.domain.req.SysUserTagEditReq;
import com.muyu.user.domain.model.SysUserTagSaveModel;
import com.muyu.user.domain.model.SysUserTagEditModel;
import com.muyu.user.service.SysUserTagService;
import com.muyu.common.core.web.page.DataPageResp;
import com.muyu.common.core.web.page.PageQueryModel;

/**
 * 用户标签Controller
 *
 * @author liuguoqiang
 * @date 2025-09-14
 */
@RestController
@RequestMapping("/tag")
@Tag(name = "用户标签", description = "用户标签控制层")
public class SysUserTagController extends BaseController {

    /**
    * 用户标签业务层
    */
    @Autowired
    private SysUserTagService sysUserTagService;

    /**
     * 查询用户标签列表
     */
    @Operation(summary = "获取用户标签列表", description = "获取用户标签列表")
    @RequiresPermissions("user:tag:list")
    @GetMapping("/list")
    public Result<DataPageResp<SysUserTagPageResp>> list(SysUserTagQueryReq sysUserTagQueryReq) {
        PageQueryModel<SysUserTagPageQueryModel> pageQueryModel = sysUserTagService.pageQuery(SysUserTagQueryModel.reqBuild(sysUserTagQueryReq));
        return Result.success(DataPageResp.of(pageQueryModel).cover(SysUserTagPageResp::infoBuild));
    }

    /**
     * 导出用户标签列表
     */
    @Operation(summary = "导出用户标签列表", description = "导出用户标签列表")
    @RequiresPermissions("user:tag:export")
    @Log(title = "用户标签", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysUserTag sysUserTag) {
        List<SysUserTag> list = sysUserTagService.exportList(sysUserTag);
        ExcelUtil<SysUserTag> util = new ExcelUtil<>(SysUserTag.class);
        util.exportExcel(response, list, "用户标签数据");
    }

    /**
     * 获取用户标签详细信息
     */
    @Operation(summary = "获取用户标签详细信息", description = "获取用户标签详细信息")
    @RequiresPermissions("user:tag:query")
    @GetMapping(value = "/{tagId}")
    public Result<SysUserTag> getInfo(@PathVariable("tagId") Long tagId) {
        return Result.success(sysUserTagService.getById(tagId));
    }

    /**
     * 新增用户标签
     */
    @RequiresPermissions("user:tag:add")
    @Log(title = "用户标签", businessType = BusinessType.INSERT)
    @PostMapping
    @Operation(summary = "新增用户标签", description = "新增用户标签")
    public Result<String> add(@RequestBody SysUserTagSaveReq sysUserTagSaveReq) {
        sysUserTagService.save(SysUserTagSaveModel.saveReqBuild(sysUserTagSaveReq));
        return Result.success();
    }

    /**
     * 修改用户标签
     */
    @RequiresPermissions("user:tag:edit")
    @Log(title = "用户标签", businessType = BusinessType.UPDATE)
    @PutMapping("/{tagId}")
    @Operation(summary = "修改用户标签", description = "修改用户标签")
    public Result<String> edit(@PathVariable(value = "tagId") Long tagId, @RequestBody SysUserTagEditReq sysUserTagEditReq) {
        sysUserTagService.edit(SysUserTagEditModel.editReqBuild(tagId,sysUserTagEditReq));
        return Result.success();
    }

    /**
     * 删除用户标签
     */
    @RequiresPermissions("user:tag:remove")
    @Log(title = "用户标签", businessType = BusinessType.DELETE)
	@DeleteMapping("/{tagIds}")
    @Operation(summary = "删除用户标签", description = "删除用户标签")
    public Result<String> remove(@PathVariable(value = "tagIds") List<Long> tagIds) {
        return toAjax(sysUserTagService.removeBatchByIds(tagIds));
    }
    /**
     * 获取用户标签下拉框选项
     */
    @Operation(summary = "获取用户标签下拉框选项", description = "获取用户标签下拉框选项")
    @GetMapping("/options")
    public Result<List<SysUserTag>> getTagOptions() {
        List<SysUserTag> tags = sysUserTagService.getTagOptions();
        return Result.success(tags);
    }
}
